package com.cloudfun.campusshare.service;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.DevicePlatform;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.vo.*;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.entity.DeviceEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Huqin on 2020/3/24
 */
@Slf4j
@Service
@Transactional
public class DeviceOpServiceDelegate implements DeviceOpService {

    @Autowired
    private List<AbstractDeviceOpService> deviceOpServiceList;

    @Autowired
    private DeviceService deviceService;

    private AbstractDeviceOpService getOpService(DeviceEntity deviceEntity) {
        DevicePlatform platform = deviceEntity.getPlatform();
        if (null == platform) {
            throw new BusinessException(Code.SYSTEM_ERROR, "设备所属平台错误");
        }
        for (AbstractDeviceOpService opService : deviceOpServiceList) {
            if (opService.supportPlatform().equals(platform)) {
                return opService;
            }
        }
        throw new BusinessException(Code.SYSTEM_ERROR, "设备所属平台错误");
    }

    @Override
    public BaseResult reset(String imei) {
        DeviceEntity deviceEntity = deviceService.getDeviceWithAuthByImei(imei);
        return getOpService(deviceEntity).reset(deviceEntity);
    }

    @Override
    public BaseResult<String> getPropertyByName(String imei, String propertyName) {
        DeviceEntity deviceEntity = deviceService.getDeviceWithAuthByImei(imei);
        return getOpService(deviceEntity).getPropertyByName(deviceEntity, propertyName);
    }

    @Override
    public BaseResult getCautionList(String imei) {
        DeviceEntity deviceEntity = deviceService.getDeviceWithAuthByImei(imei);
        return getOpService(deviceEntity).getCautionList(deviceEntity);
    }

    @Override
    public BaseResult wash(RollerWasherOpVO rollerWasherOpVO) {
        DeviceEntity deviceEntity = deviceService.getDeviceWithAuthByImei(rollerWasherOpVO.getImei());
        return getOpService(deviceEntity).wash(deviceEntity, rollerWasherOpVO);
    }

    @Override
    public BaseResult dry(DryerOperateVO deviceDryingVO) {
        DeviceEntity deviceEntity = deviceService.getDeviceWithAuthByImei(deviceDryingVO.getImei());
        return getOpService(deviceEntity).dry(deviceEntity, deviceDryingVO);
    }

    @Override
    public BaseResult pulseWash(PulseWasherOperateVO operateVO) {
        DeviceEntity deviceEntity = deviceService.getDeviceWithAuthByImei(operateVO.getImei());
        return getOpService(deviceEntity).pulseWash(deviceEntity, operateVO);
    }

    @Override
    public BaseResult shoeWash(ShoeWasherOperateVO operateVO) {
        DeviceEntity deviceEntity = deviceService.getDeviceWithAuthByImei(operateVO.getImei());
        return getOpService(deviceEntity).shoeWash(deviceEntity, operateVO);
    }

    @Override
    public BaseResult deviceOnline(String imei) {
        DeviceEntity deviceEntity = deviceService.getDeviceWithAuthByImei(imei);
        return getOpService(deviceEntity).deviceOnline(deviceEntity);
    }

    @Override
    public BaseResult<?> deviceVerifyCode(String imei, String verifyCode) {
        DeviceEntity deviceEntity = deviceService.getDeviceWithAuthByImei(imei);
        return getOpService(deviceEntity).deviceVerifyCode(deviceEntity, verifyCode);
    }

    @Override
    public BaseResult<?> pumpStart(PumpWorkVO pumpWorkVO) {
        DeviceEntity deviceEntity = deviceService.getDeviceWithAuthByImei(pumpWorkVO.getDeviceId());
        return getOpService(deviceEntity).pumpStart(deviceEntity, pumpWorkVO);
    }

    @Override
    public BaseResult<?> getProperties(String imei) {
        DeviceEntity deviceEntity = deviceService.getDeviceWithAuthByImei(imei);
        return getOpService(deviceEntity).getProperties(deviceEntity);
    }
}
